package com.ad2campaign.engine.db.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.ad2campaign.engine.core.TaskStatus;
import com.ad2campaign.engine.db.model.Keystore;
import com.ad2campaign.engine.db.model.PushNotificationMaster;
import com.ad2campaign.engine.db.rowmapper.KeystoreRowMapper;

public class PushNotificationMasterDaoImpl implements PushNotificationMasterDao 
{
	
	private JdbcTemplate jdbcTemplate;
 	private String findPushByDateQuery;
	private String findCertificateByIdQuery;
	private String findKeyStoreByCidQuery;
	private String findTaskStatusQuery;
	
 	private String updateMessageStatusAndExecutionIdByIdQuery;
 	private String updateMessageStatusAndProcucedByIdQuery;
 	private String updateMessageStatusAndExecutionIdByExecutionIdQuery;
 	private String updateMessageStatusIdQuery;
 	private RowMapper<TaskStatus> taskStatusRowmapper;
 	public RowMapper<TaskStatus> getTaskStatusRowmapper() {
		return taskStatusRowmapper;
	}

	public void setTaskStatusRowmapper(RowMapper<TaskStatus> taskStatusRowmapper) {
		this.taskStatusRowmapper = taskStatusRowmapper;
	}

	private RowMapper<PushNotificationMaster> pushNotificationMasterRowMapper;

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 	private KeystoreRowMapper keystoreRowMapper;
 	private static final Logger LOGGER = Logger.getLogger(PushNotificationMasterDaoImpl.class);
	@Override
	public List<PushNotificationMaster> findPushByDate(Date date) 
	{
		String todayDate_minstart = dateFormat.format(date)+":00";
 		String query = String.format(getFindPushByDateQuery(), todayDate_minstart);
		LOGGER.debug("findPushByDate("+todayDate_minstart+") Executing query  : ["+query+"]");
		return jdbcTemplate.query(query,pushNotificationMasterRowMapper);
 	}

	@Override
	public Keystore findKeyStoreByCid(int cId) {
		List<Keystore> keystores= jdbcTemplate.query(String.format(getFindKeyStoreByCidQuery(),cId),keystoreRowMapper);
		if (keystores!=null && keystores.size()>0)
		{
			return keystores.get(0);
		}
		else
		{
			return null;
		}
	}
	
	@Override
	public int updateMessageStatusAndExecutionIdById(int id,Long execution_id, int status) 
	{
		try {
			String query = String.format(updateMessageStatusAndExecutionIdByIdQuery,status,execution_id,id);
			LOGGER.info("updateMessageStatusAndExecutionIdById() : Executing query : "+query);
			return jdbcTemplate.update(query);
		
		} catch (Exception e) {
			LOGGER.error(e);
			LOGGER.debug(e);
			return -1;		
		}
	}
	
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public String getFindPushByDateQuery()
	{
		return findPushByDateQuery;
	}

	public void setFindPushByDateQuery(String findPushByDateQuery) 
	{
		this.findPushByDateQuery = findPushByDateQuery;
	}

	public String getFindCertificateByIdQuery() 
	{
		return findCertificateByIdQuery;
	}

	public void setFindCertificateByIdQuery(String findCertificateByIdQuery) 
	{
		this.findCertificateByIdQuery = findCertificateByIdQuery;
	}
	
	public RowMapper<PushNotificationMaster> getPushNotificationMasterRowMapper() {
		return pushNotificationMasterRowMapper;
	}

	public void setPushNotificationMasterRowMapper(	RowMapper<PushNotificationMaster>  pushNotificationMasterRowMapper) 
	{
		this.pushNotificationMasterRowMapper = pushNotificationMasterRowMapper;
	}

	
 
	public String getFindKeyStoreByCidQuery()
	{
		return findKeyStoreByCidQuery;
	}

	public void setFindKeyStoreByCidQuery(String findKeyStoreByCidQuery) {
		this.findKeyStoreByCidQuery = findKeyStoreByCidQuery;
	}

	 
 
	public KeystoreRowMapper getKeystoreRowMapper() {
		return keystoreRowMapper;
	}

	public void setKeystoreRowMapper(KeystoreRowMapper keystoreRowMapper) {
		this.keystoreRowMapper = keystoreRowMapper;
	}

	@Override
	public int updateMessageStatusAndProcucedById(int id, int status, int produced) {
		String query = String.format(updateMessageStatusAndProcucedByIdQuery, produced,status, id);
		try 
		{
			LOGGER.info( "updateMessageStatusAndProcucedById(): Executing query : "+query);
			return jdbcTemplate.update(query);
	
		}
		catch (Exception e) 
		{
			LOGGER.error(e);
			LOGGER.debug(e);
			return -1;
		}
		

	}

	
 

 	public String getupdateMessageStatusAndProcucedByIdQuery() {
		return updateMessageStatusAndProcucedByIdQuery;
	}

	public void setupdateMessageStatusAndProcucedByIdQuery(
			String updateMessageStatusAndProcucedByIdQuery) {
		this.updateMessageStatusAndProcucedByIdQuery = updateMessageStatusAndProcucedByIdQuery;
	}

	public String getUpdateMessageStatusAndExecutionIdByIdQuery() {
		return updateMessageStatusAndExecutionIdByIdQuery;
	}

	public void setUpdateMessageStatusAndExecutionIdByIdQuery(
			String updateMessageStatusAndExecutionIdByIdQuery) {
		this.updateMessageStatusAndExecutionIdByIdQuery = updateMessageStatusAndExecutionIdByIdQuery;
	}

	@Override
	public TaskStatus findTaskStatus(long executionId) 
	{
 		String query = String.format(findTaskStatusQuery, executionId);
 		try 
 		{
 			
 			
 			List<TaskStatus> status =jdbcTemplate.query(query, taskStatusRowmapper);
 			if (status.size()>0)
 			{
   		       return	jdbcTemplate.query(query, taskStatusRowmapper).get(0);

 			}
 			else
 			{
 				return TaskStatus.UNKNOWN;

 			}
 			
		} catch (Exception e) 
		{
			LOGGER.error(String.format("findTaskStatus(%d): "+e.getMessage(),executionId));
			LOGGER.debug(e);
			return TaskStatus.UNKNOWN;
		}
 		
  		
	}


	public String getFindTaskStatusQuery() {
		return findTaskStatusQuery;
	}

	public void setFindTaskStatusQuery(String findTaskStatusQuery) {
		this.findTaskStatusQuery = findTaskStatusQuery;
	}

	public String getUpdateMessageStatusAndExecutionIdByExecutionIdQuery() {
		return updateMessageStatusAndExecutionIdByExecutionIdQuery;
	}
	

	public void setUpdateMessageStatusAndExecutionIdByExecutionIdQuery(
			String updateMessageStatusAndExecutionIdByExecutionIdQuery) {
		this.updateMessageStatusAndExecutionIdByExecutionIdQuery = updateMessageStatusAndExecutionIdByExecutionIdQuery;
	}


 
	
	@Override
	public int updateMessageStatusAndExecutionIdByExecutionId(Long oldExecutionId,
			Long executionId, int status) {
		try {
			String query = String.format(updateMessageStatusAndExecutionIdByExecutionIdQuery,status,executionId,oldExecutionId);
			LOGGER.info("updateMessageStatusAndExecutionIdByExecutionId() : Executing query : "+query);
			return jdbcTemplate.update(query);
		
		} catch (Exception e) {
			LOGGER.error(e);
			LOGGER.debug(e);
			return -1;		
		}
		
 	}

	@Override
	public int updateMessageStatusId(int id, TaskStatus status) {
		// TODO Auto-generated method stub
		String query = String.format(updateMessageStatusIdQuery, status.getStatusValue(), id);
		try 
		{
			LOGGER.info( "updateMessageStatusId(): Executing query : "+query);
			return jdbcTemplate.update(query);
	
		}
		catch (Exception e) 
		{
			LOGGER.error(e);
			LOGGER.debug(e);
			return -1;
		}
		
	}
	
 	public String getUpdateMessageStatusIdQuery() {
		return updateMessageStatusIdQuery;
	}

	public void setUpdateMessageStatusIdQuery(String updateMessageStatusIdQuery) {
		this.updateMessageStatusIdQuery = updateMessageStatusIdQuery;
	}
}
